package com.assistant.aspect;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.assistant.vo.resp.AjaxResult;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
@Aspect
public class RequestLogAspect {
    private final Logger logger = LoggerFactory.getLogger(RequestLogAspect.class);

    /**
     * 定义切点
     */
    @Pointcut("execution(* com.assistant.controller..*(..))")
    public void requestServer() {
    }

    @Around("requestServer()")
    public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
        //记录请求开始执行时间：
        long beginTime = System.currentTimeMillis();
        //获取请求信息
        ServletRequestAttributes sra = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = sra.getRequest();

        //获取代理地址、请求地址、请求类名、方法名
        String requestURI = request.getRequestURI();
        logger.info("请求接口开始:{}",requestURI);
        String methodName = pjp.getSignature().getName();
        String clazzName = pjp.getTarget().getClass().getSimpleName();

        //获取请求参数：
        MethodSignature ms = (MethodSignature) pjp.getSignature();
        //获取请求参数类型
        String[] parameterNames = ms.getParameterNames();
        //获取请求参数值
        Object[] parameterValues = pjp.getArgs();
        StringBuilder sb = new StringBuilder();
        //组合请求参数，进行日志打印
        if (parameterNames != null && parameterNames.length > 0) {
            for (int i = 0; i < parameterNames.length; i++) {
                if (parameterValues[i] instanceof MultipartHttpServletRequest || parameterValues[i] instanceof MultipartFile){
                    continue;
                }
                if (parameterNames[i].equals("bindingResult")) {
                    break;
                }
                if (parameterValues[i]==null){
                    continue;
                }
                if ((parameterValues[i] instanceof HttpServletRequest) || (parameterValues[i] instanceof HttpServletResponse)) {
                    sb.append("[").append(parameterNames[i]).append("=").append(parameterValues[i]).append("]");
                } else {
                    sb.append("[").append(parameterNames[i]).append("=").append(JSON.toJSONString(parameterValues[i], SerializerFeature.WriteDateUseDateFormat)).append("]");
                }
            }
        }
        Object result = null;
        try {
            result = pjp.proceed();
        } catch (Throwable throwable) {
            //请求操纵失败
            //记录错误日志
            logger.error("(切面处理请求错误！" +
                            "URI信息(ง•̀_•́)ง->：【{}】 请求映射控制类(ง•̀_•́)ง->：【{}】 " +
                            "请求方法(ง•̀_•́)ง->：【{}】 请求参数列表(ง•̀_•́)ง->：【{}】"
                    , requestURI, clazzName, methodName, sb,throwable);
            throw throwable;
        }
        //请求操作成功
        String resultJosnString = "";
        if (result != null) {
            if (result instanceof AjaxResult) {
                AjaxResult ajaxResult = (AjaxResult)result;
                ajaxResult.put("traceId", MDC.get("THREAD_ID"));
                resultJosnString = JSON.toJSONString(ajaxResult);
            } else {
                resultJosnString = String.valueOf(result);
            }
        }
        //记录请求完成执行时间：
        long endTime = System.currentTimeMillis();
        long usedTime = endTime - beginTime;
        //记录日志
        logger.info("请求操作成功!请求耗时:【{}】,URI信息->:【{}】,请求映射控制接口->:【{}】\n" +
                    "请求参数列表->:【{}】\n" +
                    "返回值->:【{}】",
                usedTime, requestURI, clazzName+"."+methodName, sb, resultJosnString);

        return result;
    }
}